package file.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;

import file.FileChanger;

public class GUI extends JFrame implements ActionListener {
	
	/**基礎となるGUIパーツ
	 * 上画面のテキストフィールド、下画面のパネル、
	 * 下画面のスクロールバーをフィールドへ
	 */
	private JTextField txtf;
	private JPanel panel;
	private JScrollPane scrollpanel;
	private JFileChooser fileChooser = new JFileChooser();
	
	/**コンストラクタ
	 * レイアウト、終了ボタン、タイトル、サイズ、位置をセットし表示
	 * @throws IOException
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws UnsupportedLookAndFeelException
	 */
	GUI() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		makeLayout();
		makeMenu();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("viewer");
		setSize(800, 600);
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	/**レイアウトの作成
	 * 上下に分割し、フィールドの値の関係を作成及び初期値を作成
	 * @throws IOException
	 */
	private void makeLayout() throws IOException{
		JSplitPane versplit1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		JSplitPane versplit2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		
		JButton book = new JButton(new ImageIcon("book.png"));
		JButton castle = new JButton(new ImageIcon("castle.png"));
		book.setActionCommand("OPEN");
		book.addActionListener(this);
		castle.setActionCommand("FOLDER");
		castle.addActionListener(this);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(true);
		toolBar.add(book);
		toolBar.add(castle);
		
		txtf = new JTextField();
		txtf.setText("none");
		txtf.setEditable(false);
		
		panel = new JPanel();
		scrollpanel = new JScrollPane(panel);
		scrollpanel.getVerticalScrollBar().setUnitIncrement(30);
		scrollpanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		versplit2.setTopComponent(txtf);
		versplit2.setBottomComponent(scrollpanel);
		versplit1.setTopComponent(toolBar);
		versplit1.setBottomComponent(versplit2);
		getContentPane().add(versplit1);
	}
	
	@SuppressWarnings("deprecation")
	private void makeMenu() {
		JMenuBar bar = new JMenuBar();
		getRootPane().setMenuBar(bar);
		
		JMenu fileMenu = new JMenu("ファイル(F)");
		fileMenu.setMnemonic('F');
		bar.add(fileMenu);
		
		JMenuItem fileOpen = new JMenuItem("開く(O)");
		fileOpen.setMnemonic('O');
		fileOpen.setActionCommand("OPEN");
		fileOpen.addActionListener(this);
		fileMenu.add(fileOpen);
		
		JMenuItem homeFolder = new JMenuItem("フォルダを開く(H)");
		homeFolder.setMnemonic('H');
		homeFolder.setActionCommand("FOLDER");
		homeFolder.addActionListener(this);
		fileMenu.add(homeFolder);
		
		// [ファイル]-[終了]
		JMenuItem menuExit = new JMenuItem("終了(X)");
		menuExit.setMnemonic('X');
		menuExit.setActionCommand("EXIT");
		menuExit.addActionListener(this);
		fileMenu.add(menuExit);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		String com = ae.getActionCommand();
		if(com=="OPEN"){
			com_open();
		}else if(com=="FOLDER"){
			com_folder();
		}else if(com=="EXIT"){
			com_exit();
		}else if(com==""){
			
		}else if(com==""){
			
		}else{
			
		}
	}
	
	private void com_open() {
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int val = fileChooser.showOpenDialog(this);
		if(val==JFileChooser.APPROVE_OPTION){
			File file = fileChooser.getSelectedFile();
			getFolder(file);
		}
	}

	private void com_folder() {
		// TODO Auto-generated method stub
		
	}

	private void com_exit() {
		System.exit(0);
	}

	/**フォルダ内の画像ファイルをすべて並べて表示
	 * フォルダ内からjpgファイルを探索し、間に空白を並べてpanel内に表示する
	 * @param folder 探索するフォルダ
	 */
	public void getFolder(File folder){
		txtf.setText(folder.getName());
		panel.removeAll();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		String[] files = FileChanger.fileNames(folder.getPath());
		files = FileChanger.specificSort(files, FileChanger.EXTENTION_JPG);
		JLabel[] label = new JLabel[files.length];
		for(int i=0;i<files.length;i++){
			label[i] = new JLabel(new ImageIcon(files[i]));
			panel.add(label[i]);
			JLabel lab = new JLabel(" ");
			panel.add(lab);
		}
		scrollpanel.getVerticalScrollBar().setValue(0);
		scrollpanel.getHorizontalScrollBar().setValue(0);
		scrollpanel.validate();
	}
	
	public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
		new GUI();
	}
}
